class AgreementsController < ApplicationController
  
  before_filter :require_user
  
  # GET /agreements
  # GET /agreements.json
  def index
    authorize! :read, Agreement
    # @agreements = Agreement.all
    @search = Agreement.search(params[:search])
    @agreements = @search.paginate(:per_page => 5,:page => params[:page])
    
    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @agreements }
    end
  end

  # GET /agreements/1
  # GET /agreements/1.json
  def show
    authorize! :read, Agreement
    @agreement = Agreement.find(params[:id])
    @read_only = true
    
    @customers = Customer.find(:all)    
    @customer_list = Array.new
    @customers.each do |customer|
      @customer_list << customer.to_combo
    end
    
    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @agreement }
    end
  end

  # GET /agreements/new
  # GET /agreements/new.json
  def new
    authorize! :create, Agreement
    @agreement = Agreement.new
    
    @customers = Customer.find(:all)    
    @customer_list = Array.new
    @customers.each do |customer|
      @customer_list << customer.to_combo
    end

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @agreement }
    end
  end

  # GET /agreements/1/edit
  def edit
    authorize! :update, Agreement
    @agreement = Agreement.find(params[:id])
    
    @customers = Customer.find(:all)    
    @customer_list = Array.new
    @customers.each do |customer|
      @customer_list << customer.to_combo
    end
    
  end

  # POST /agreements
  # POST /agreements.json
  def create
    authorize! :create, Agreement
    @agreement = Agreement.new(params[:agreement])
    @agreement.user_log = current_user.login
    respond_to do |format|
      if @agreement.save
        format.html { redirect_to @agreement, notice: 'Agreement was successfully created.' }
        format.json { render json: @agreement, status: :created, location: @agreement }
      else
        format.html { render action: "new" }
        format.json { render json: @agreement.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /agreements/1
  # PUT /agreements/1.json
  def update
    authorize! :update , Agreement
    @agreement = Agreement.find(params[:id])
    @agreement.user_log = current_user.login
    respond_to do |format|
      if @agreement.update_attributes(params[:agreement])
        format.html { redirect_to @agreement, notice: 'Agreement was successfully updated.' }
        format.json { head :ok }
      else
        format.html { render action: "edit" }
        format.json { render json: @agreement.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /agreements/1
  # DELETE /agreements/1.json
  def destroy
    authorize! :destory , Agreement
    @agreement = Agreement.find(params[:id])
    @agreement.user_log = current_user.login
    @agreement.destroy

    respond_to do |format|
      format.html { redirect_to agreements_url }
      format.json { head :ok }
    end
  end
end
